import numpy as np
import pandas as pd
import plotly.graph_objects as go
life = pd.read_csv('NCHS_-_Death_rates_and_life_expectancy_at_birth.csv')
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(life)
black2017 = life[(life.Sex == 'Both Sexes') & (life.Race == 'Black') & (life.Year == 2017)]
white2017 = life[(life.Sex == 'Both Sexes') & (life.Race == 'White') & (life.Year == 2017)]
black1900 = life[(life.Sex == 'Both Sexes') & (life.Race == 'Black') & (life.Year == 1900)]
white1900 = life[(life.Sex == 'Both Sexes') & (life.Race == 'White') & (life.Year == 1900)]
black2017M = life[(life.Sex == 'Male') & (life.Race == 'Black') & (life.Year == 2017)]
black2017F = life[(life.Sex == 'Female') & (life.Race == 'Black') & (life.Year == 2017)]
white2017M = life[(life.Sex == 'Male') & (life.Race == 'White') & (life.Year == 2017)]
white2017F = life[(life.Sex == 'Female') & (life.Race == 'White') & (life.Year == 2017)]
black1900M = life[(life.Sex == 'Male') & (life.Race == 'Black') & (life.Year == 1900)]
black1900F = life[(life.Sex == 'Female') & (life.Race == 'Black') & (life.Year == 1900)]
white1900M = life[(life.Sex == 'Male') & (life.Race == 'White') & (life.Year == 1900)]
white1900F = life[(life.Sex == 'Female') & (life.Race == 'White') & (life.Year == 1900)]
black2017MLE = black2017M.iloc[0,3]
black2017FLE = black2017F.iloc[0,3]
white2017MLE = white2017M.iloc[0,3]
white2017FLE = white2017F.iloc[0,3]
black1900MLE = black1900M.iloc[0,3]
black1900FLE = black1900F.iloc[0,3]
white1900MLE = white1900M.iloc[0,3]
white1900FLE = white1900F.iloc[0,3]
blackMDiff = black2017MLE - black1900MLE
blackFDiff = black2017FLE - black1900FLE
whiteMDiff = white2017MLE - white1900MLE
whiteFDiff = white2017FLE - white1900FLE
blackMDiffPer = ((black2017MLE - black1900MLE)/black1900MLE)*100
blackFDiffPer = ((black2017FLE - black1900FLE)/black1900FLE)*100
whiteMDiffPer = ((white2017MLE - white1900MLE)/white1900MLE)*100
whiteFDiffPer = ((white2017FLE - white1900FLE)/white1900FLE)*100
Increase of life expectancy of black in the course of 117 years, as shown in the two bar charts below.
In the first bar chart, it displays the increase in life expectancy of black and white in male and female in the course of 117 years.
Black male’s life expectancy had increased 39 years, while the white male counterpart had 29 years.
The increase in number of years of black male is 10 years more than that of white male.
Black female’s life expectancy had increased 45 years, while the white female counterpart had 32 years.
The increase in number of years of black female is 13 years more than that of white female.
import plotly.graph_objects as go
lifeRace = ['Black', 'White']
fig = go.Figure(data=[
go.Bar(name='Male', x=lifeRace, y=[blackMDiff, whiteMDiff]),
go.Bar(name='Female', x=lifeRace, y=[blackFDiff, whiteFDiff])
])
# Change the bar mode
fig.update_layout(xaxis_title='Race', yaxis_title='differences in life expectancy',
title='Years Increase in Life Epectancy 1900-2017', barmode='group',
width=700, height=500)
fig.show()
In the second bar chart as below, it displays the % increase in life expectancy of black and white in male and female in the course of 117 years.
Black male’s life expectancy had increased 121%, while the white male counterpart had increased 63% years.
Black female’s life expectancy had increased 134%, while the white female counterpart had increased 66%.
The percentage increase of black is double of that of white.
lifeRace = ['Black', 'White']
fig = go.Figure(data=[
go.Bar(name='Male', x=lifeRace, y=[round(blackMDiffPer, 1), round(whiteMDiffPer, 1)]),
go.Bar(name='Female', x=lifeRace, y=[round(blackFDiffPer, 1), round(whiteFDiffPer, 1)])
])
# Change the bar mode
fig.update_layout(xaxis_title='Race', yaxis_title='% differences in life expectancy',
title='% Increase in Life Expectancy 1900-2017', barmode='group',
width=700, height=500)
fig.show()
# Data
blackMale = life[(life.Sex == 'Male') & (life.Race == 'Black')]
blackFemale = life[(life.Sex == 'Female') & (life.Race == 'Black')]
whiteMale = life[(life.Sex == 'Male') & (life.Race == 'White')]
whiteFemale = life[(life.Sex == 'Female') & (life.Race == 'White')]
# Create figure
fig = go.Figure()
# Add traces
fig.add_trace(go.Scatter(x=blackMale['Year'], y=blackMale['Average Life Expectancy (Years)'],
mode='lines', name='Black Male', line=dict(color='darkblue')))
fig.add_trace(go.Scatter(x=blackFemale['Year'], y=blackFemale['Average Life Expectancy (Years)'],
mode='lines', name='Black Female', line=dict(color='crimson')))
fig.add_trace(go.Scatter(x=whiteMale['Year'], y=whiteMale['Average Life Expectancy (Years)'],
mode='lines', name='White Male', line=dict(color='blue')))
fig.add_trace(go.Scatter(x=whiteFemale['Year'], y=whiteFemale['Average Life Expectancy (Years)'],
mode='lines', name='White Female', line=dict(color='magenta')))
# Update layout
fig.update_layout(
xaxis_title='Year',
yaxis_title='Average Life Expectancy (Years)',
title='Change in Life Expectancy of Black and White Males and Females (1900-2017)',
hovermode='closest'
)
# Show figure
fig.show()
The line graph above shows how the trend of life expectancy of black male, black female, white male and white female compare to each other from 1900 to 2017
Life expectancy overall has increased from 47 years to 79 years in 117 years.
Before 1945, the changes had been very volatile due to infectious diseases, war and Great Depression. After 1945, the advancements in medicine has caused the trend to be more stable.
Black always have lower life expectancy than white. However, the gap has been narrowing significantly in the last 117 years due to the improvement in racial equality. Convergence of factors includes access to health care, education, income, etc.
In 1965, life expectancy of black female has crossed above that of white male.
In around 2000, life expectancy of black female and white male became the same.
Afterwards, life expectancy of black female continues to be higher than that of white male again, and gradually closing the gap with white female.
Female of the same race always has a higher life expectancy of their male counterpart. The gap had been widened and narrowed due to World War II and men quitting smoking in 1960s.